package redtankface;

import robocode.Robot;

/**
 * TankUtils
 *
 * @author: k.krestnikov
 * @date: 30.09.2010
 */
public class TankUtils {
	public static double pi = 3.1415926535;

	public static double getDistance(TankCoordinates a1, TankCoordinates a2) {
		return Math.sqrt((a1.mX - a2.mX) * (a1.mX - a2.mX) + (a1.mY - a2.mY) * (a1.mY - a2.mY));
	}

	public static double getAngle(TankCoordinates a1, TankCoordinates a2) {
		double dx = Math.abs(a1.mX - a2.mX);
		double dy = Math.abs(a1.mY - a2.mY);

		boolean xm = (a2.mX - a1.mX) < 0;
		boolean ym = (a2.mY - a1.mY) < 0;
		if (!xm && !ym) {
			return Math.atan(dx / dy);
		} else if (!xm && ym) {
			return Math.atan(dy / dx) + Math.PI / 2;
		} else if (xm && ym) {
			return Math.atan(dx / dy) + Math.PI;
		} else if (xm && !ym) {
			return Math.atan(dy / dx) + 3 * Math.PI / 2;
		}
		return 0;
	}

	/**
	 * Возвращает расстояние до стены по заданному направлению
	 * @param a1
	 * @param angle
	 * @param aRobot
	 * @return
	 */
	public static double getDistanceToWall(TankCoordinates a1, double angle, Robot aRobot) {
		double r1 = (aRobot.getBattleFieldHeight() - a1.mY) / Math.cos(angle);
		double r2 = (a1.mY) / Math.cos(angle);
		double r3 = (aRobot.getBattleFieldWidth() - a1.mX) / Math.sin(angle);
		double r4 = (a1.mX) / Math.sin(angle);
		double toY = Math.max(r1, r2);
		double toX = Math.max(r3, r4);
		return Math.min(toX, toY);
	}
}
